import { customRef } from 'vue'
import { type App } from 'vue'
// 全局加载计数器
const useLoadCount = (app: App): void => {
  const loadCount = customRef((track, trigger) => {
    let value = 0
    return {
      get() {
        track()
        return value > 0
      },
      set(newValue) {
        if (newValue) {
          value++
        } else {
          value--
          // 限制最小值为0
          value = Math.max(0, value)
        }
        trigger()
      },
    }
  })
  app.config.globalProperties.$isLoading = loadCount
  window.$isLoading = loadCount
  console.log('中源来 loadCount installed')
}
export default useLoadCount
